package com.st.sbmb.controller.rmqcontroller;

import com.st.sbmb.config.rmqconfig.DirectRabbitConfig;
import com.st.sbmb.controller.mbcontroller.MybatisController;
import com.st.sbmb.mapper.UserMapper;
import com.st.sbmb.pojo.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/direct")
public class DirectController {
    @Autowired
    UserMapper userMapper;

    private final RabbitTemplate rabbitTemplate;

    public DirectController(RabbitTemplate rabbitTemplate) {
        this.rabbitTemplate = rabbitTemplate;
    }

/**
 * direct交换机为直连模式交换机
 *      根据消息携带的路由键将消息投递给对应队列
 *
 *
 * @return
*/
    @GetMapping("send")
    public Object sendMsg(){

        List<User> userList = new ArrayList<>();
        User user = new User();
        int id = 1;
        user = userMapper.findUserById(id);
        userList = userMapper.findAllUser();


        rabbitTemplate.convertAndSend(DirectRabbitConfig.DIRECT_EXCHANGE, DirectRabbitConfig.DIRECT_ROUTING_KEY, "发送一条测试消息：direct");
        //return "direct消息发送成功！！" + user + userList;
        return userList;
    }
}
